<!-- 版本检测 -->
<template>
	<div style="display: none"></div>
</template>

<script setup>
	import { onMounted, onUnmounted } from 'vue'
	import { notification } from 'ant-design-vue'

	const CURRENT_VERSION = '0.0.104' // 当前版本号
	let intervalTimer = null

	const checkVersion = async () => {
		fetch('/versionCheck.json?t=' + new Date().getTime())
			.then((res) => res.json())
			.then((data) => {
				const serverVersion = data.version
				// console.log('serverVersion', serverVersion)
				if (serverVersion !== CURRENT_VERSION) {
					notification.info({
						message: '版本检测',
						description: data.updateContent
					})
					clearInterval(intervalTimer)
					intervalTimer = null
				}
			})
	}

	onMounted(() => {
		// 如果是开发环境，检查版本更新
		if (process.env.NODE_ENV === 'production') {
			// 初次检查
			checkVersion()
			// 每15秒检查一次
			intervalTimer = setInterval(checkVersion, 15000)
		}
	})
	onUnmounted(() => {
		if (intervalTimer) {
			clearInterval(intervalTimer)
			intervalTimer = null
		}
	})
</script>
